package test6;

public class Demo6 {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(5);
        root.left.left = new TreeNode(3);
        root.left.right = new TreeNode(4);
        //root.right.left = new TreeNode(6);
        root.right.right = new TreeNode(6);
        traverse(root);
        System.out.println(root);
    }

    static void traverse(TreeNode node) {
        if (node == null) return;
        traverse(node.left);
        TreeNode left = node.left;
        node.left = null;
        if (left != null) {
            TreeNode temp = node.right;
            node.right = left;
            TreeNode r = node;
            while (r.right != null) r = r.right;
            r.right = temp;
        }
        traverse(node.right);
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
